﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using System.Security.Cryptography;
using System.Text;
namespace BLL.TuyenDung
{
    // status : 0 -> chua kich hoat
    // statuc : 1 -> da kich hoat
    public class UserHelper
    {
        public static string MaHoa(string a) // ma hoa
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] chuoi = Encoding.ASCII.GetBytes(a);
                byte[] mahoa = md5.ComputeHash(chuoi);
                return Convert.ToBase64String(mahoa).Trim();
            }
            catch
            {
                return null;
            }
        }
        public static int KiemTraEmailTonTai(string Email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = db.UserTuyenDungs.FirstOrDefault(k=>k.Email==Email);
            if (query != null)
            {
                return 1;
            }
            else return -1;
        }
        public static void Add_UserTuyenDung(string Email, string MatKhau, string TenCongTy, string SoLuocCty, string SoNV, string website, string Logo, string TenLienHe, string DiaChi, int? TinhId, string DienThoai, string Fax, string EmailLienHe, string NguoiLienHe, string DienThoaiLienHe, string ActiveCode)
        { 
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = new UserTuyenDung();
            user.Email = Email;
            user.MatKhau = MaHoa(MatKhau);
            user.TenCongTy = TenCongTy;
            user.SoLuocCongTy = SoLuocCty;
            user.SoNhanVien = SoNV;
            user.Website = website;
            if (Logo != null)
            {
                user.UrlLogo = Logo;
            }
            user.TenLienHe = TenLienHe;
            user.DiaChi = DiaChi;
            user.TinhId = TinhId;            
            user.DienThoai = DienThoai;
            user.Fax = Fax;
            user.EmailLienHe = EmailLienHe;
            user.NguoiLienHe = NguoiLienHe;
            user.DienThoaiLienHe = DienThoaiLienHe;
            user.Status = 0;
            user.ActiveCode = ActiveCode;
            db.UserTuyenDungs.InsertOnSubmit(user);
            db.SubmitChanges();
        }
        public static void Update_UserTuyenDung(int UserId,string TenCongTy, string SoLuocCty, string SoNV, string website, string Logo, string TenLienHe, string DiaChi, int? TinhId, string DienThoai, string Fax, string EmailLienHe, string NguoiLienHe, string DienThoaiLienHe)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k=>k.UserId==UserId);            
            user.TenCongTy = TenCongTy;
            user.SoLuocCongTy = SoLuocCty;
            user.SoNhanVien = SoNV;
            user.Website = website;
            if (Logo != null)
            {
                user.UrlLogo = Logo;
            }
            user.TenLienHe = TenLienHe;
            user.DiaChi = DiaChi;
            user.TinhId = TinhId;
            
            user.DienThoai = DienThoai;
            user.Fax = Fax;
            user.EmailLienHe = EmailLienHe;
            user.NguoiLienHe = NguoiLienHe;
            user.DienThoaiLienHe = DienThoaiLienHe;                        
            db.SubmitChanges();
        }
        public static UserTuyenDung Get_UserId(int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.UserId == UserId);
            return user;
        }
        public static UserTuyenDung Get_UserEmail(string Email)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.Email == Email);
            return user;
        }
        public static bool KichHoatTaiKhoan(string Email, string ActiveCode)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.Email == Email && k.ActiveCode==ActiveCode && k.Status==0);
            if (user != null)
            {
                user.Status = 1;
                user.ActiveCode = Tool.MaNgauNhien_SoChu(5);
                db.SubmitChanges();
                return true;
            }
            else return false;
        }
        public static void DoiThongTinDangNhap(string EmailOld, string MatKhau, string EmailNew)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.Email == EmailOld && k.MatKhau == MaHoa(MatKhau) && k.Status == 1);
            if (user != null)
            {
                user.Email = EmailNew;
                db.SubmitChanges();
            }
        }
        public static void DoiMatKhau(string MatKhauCu, string MatKhauMoi)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.MatKhau == MaHoa(MatKhauCu) && k.Status == 1);
            if (user != null)
            {
                user.MatKhau = MatKhauMoi;
                db.SubmitChanges();
            }
        }
        public static void KhoaTaiKhoan(int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.UserId == UserId);
            user.Status = 0;
            db.SubmitChanges();
        }
        public static bool Login(string Email, string MatKhau)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            UserTuyenDung user = db.UserTuyenDungs.FirstOrDefault(k => k.Email == Email && k.MatKhau== Tool.MaHoa(MatKhau) && k.Status==1);
            if (user != null)
            {
                return true;
            }
            else return false;
        }
    }
}